<template>
	<view class="addEditForm">
		<view class="data-form bgfff">
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">供餐单位</text>
				<view class="data-form-input" @click="openSelect">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.unitName"
						:placeholder="optionDisabled?'-':'请选择供餐单位'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">供餐食品名称</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.foodName"
						:placeholder="optionDisabled?'-':'请输入供餐食品名称'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">供餐数量</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.quantity"
						:placeholder="optionDisabled?'-':'请输入供餐数量'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">单位</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.unit"
						:placeholder="optionDisabled?'-':'请输入单位'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">配送人姓名</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.deliveryName"
						:placeholder="optionDisabled?'-':'请输入配送人姓名'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">配送人手机号</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.deliveryPhone"
						:placeholder="optionDisabled?'-':'请输入配送人手机号'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">配送时间</text>
				<view class="data-form-input">
					<cpdd-date v-model="dataForm.deliveryTime" :disabled="optionDisabled" :isTime="true"></cpdd-date>
				</view>
			</view>
		</view>
		<view class="right-area-footer2 flex-ac bgfff" v-if="!optionDisabled">
			<view class="saveBtn cancleBtn" @tap="handleCancle">取消</view>
			<view class="saveBtn" @tap="handleSubmit">保存</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				did: '',
				dataForm: { //表单
					id: '',
					unitId: '',
					unitName: '',
					foodName: '',
					quantity: '',
					unit:'',
					deliveryName: '',
					deliveryPhone: '',
					deliveryTime: ''
				},
				cpddDateRangeVal: '',
				cpddDateRangeVal2: '',
				selectList: [], //上一页带过来，如果上一页用不到，可以在这里调用赋值
				optionDisabled: false,
			}
		},
		onLoad(options) {
			console.log('options', options);
			this.did = options.id || ''
			this.optionDisabled = options.op == 'look' ? true : false
			let title = options.op == 'look' ? '详情' : options.op == 'add' ? '新增' : '编辑'
			uni.setNavigationBarTitle({
				title
			})
			if (options.op != 'add') {
				this.loadDetail();
			}
			uni.$on('selectGcdw', (data) => {
				console.log(data);
				this.dataForm.unitId = data.id;
				this.dataForm.unitName = data.name;
				this.$forceUpdate()
			})
		},
		onUnload() {
			uni.$off('selectGcdw')
		},
		methods: {
			loadDetail() {
				this.$http.get(`/archives/record/${this.did}`).then(res => {
					if (res.code == 200) {
						Object.keys(this.dataForm).forEach(key => this.dataForm[key] = res.data[key])
						this.$refs.uploadimgcom.setItems([res.data.recordFile]);
					}
				})
			},
			openSelect() {
				this.$u.to(`/pages/gysda/xwgcdwgl-warp/index?type=select`)
			},
			handleCancle() {
				this.$u.back()
			},
			handleSubmit() {
				if(!this.dataForm.unitId) return this.$u.msg('请选择供餐单位');
				if(!this.dataForm.foodName) return this.$u.msg('请输入供应食品名称');
				if(!this.dataForm.quantity) return this.$u.msg('请输入供餐数量');
				if(!this.dataForm.unit) return this.$u.msg('请输入单位');
				if(!this.dataForm.deliveryName) return this.$u.msg('请输入配送人姓名');
				if(!this.dataForm.deliveryPhone) return this.$u.msg('请输入配送人手机号');
				if(!this.dataForm.deliveryTime) return this.$u.msg('请选择配送日期');
				let params = Object.assign({}, this.dataForm)
				params.deliveryTime += ':00'
				if (this.dataForm.id) {
					this.$http.put('/archives/record', params).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('修改成功')
							this.$u.back(1, 1000)
						}
					})
				} else {
					this.$http.post('/archives/record', params).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('已新增')
							this.$u.back(1, 1000)
						}
					})
				}
			}

		}
	}
</script>

<style>

</style>